//
//  MyApplication.m
//  ApplicationDemo
//
//  Created by 鹏鹏 on 2022/5/31.
//

#import "MyApplication.h"

@implementation MyApplication

/// 特定时间静置手机
static const NSInteger NoEventDelay = 10;//3*60;

- (instancetype)init {
    if (self = [super init]) {
        [self resetTimerPostNoEventAfterDelay];
    }
    return self;
}

- (void)postNoEventAfterDelay {
    NSLog(@"MyApplication: no events after delay: %ld", NoEventDelay);

    [[NSNotificationCenter defaultCenter] postNotificationName:@"abcdefghijklmn" object:nil];
}

- (void)cancelPostNoEventAfterDelay {
    [MyApplication cancelPreviousPerformRequestsWithTarget:self selector:@selector(postNoEventAfterDelay) object:nil];
}

- (void)resetTimerPostNoEventAfterDelay {
    [self cancelPostNoEventAfterDelay];
    [self performSelector:@selector(postNoEventAfterDelay) withObject:nil afterDelay:NoEventDelay];
}

- (void)sendEvent:(UIEvent *)event {
    NSLog(@"%s", __func__);

    if (event.type != UIEventTypeTouches) {
        [super sendEvent:event];
        return;
    }

    // touches only
    BOOL restartTimer = YES;
    for (UITouch *touch in event.allTouches) {
        if (touch.phase != UITouchPhaseCancelled && touch.phase != UITouchPhaseEnded) {
            restartTimer = NO;
            break;
        }
    }

    if (restartTimer) {
        NSLog(@"to restartTimer");
        [self resetTimerPostNoEventAfterDelay];
    }

    [super sendEvent:event];
}

@end
